# from flask import Blueprint, request
# from service.ppt_service import generate_outline
# from func.ppt_generator_v2 import generate_presentation
# from pojo.result import Result
#
#
# ppt = Blueprint('ppt', __name__, url_prefix='/ppt')
#
#
# @ppt.route('/outline', methods=['POST'])
# def outline():
#     """
#     根据用户提供的文本内容，生成ppt大纲
#     :return:
#     """
#     if request.method == 'POST':
#         data = request.get_json()
#         if data is None:
#             return Result(400, '参数错误', None).to_response()
#         text = data['text']
#         out_line = generate_outline(text)
#         return Result(200, '大纲生成成功', out_line).to_response()
#
#
# @ppt.route('/pptGenerate', methods=['POST'])
# def pptGenerate():
#     """
#     根据大纲内容，生成ppt
#     :return:
#     """
#     if request.method == 'POST':
#         data = request.get_json()
#         if data is None:
#             return Result(400, '参数错误', None).to_response()
#         # out_line = data['outline']
#
#         text = data["text"]
#         name = data["name"]
#         template_id = data['template_id']
#
#         out_line = generate_outline(text)
#
#         data = generate_presentation(out_line, template_id, text, name)
#         return Result(200, '生成成功', data).to_response()


from service.ppt_service import generate_outline
from func.ppt_generator_v2 import generate_presentation
from pojo.result import Result
from fastapi import APIRouter, HTTPException, UploadFile, File, Form, Request
from utils.log import logger

ppt = APIRouter(prefix="/ppt")

@ppt.post("/outline")
async def outline(request: Request):

    """
    根据用户提供的文本内容，生成ppt大纲
    """

    data = await request.json()
    if not data:

        return Result(400, '参数错误', None).to_response()

    text = data.get('text')
    if not text:
        return Result(400, '缺少text文本参数', None).to_response()

    out_line = generate_outline(text)
    return Result(200, '大纲生成成功', out_line).to_response()


@ppt.post("/pptGenerate")
async def ppt_generate(
    file: UploadFile = File(...),         # 接收上传的文件
    template_id: str = Form(...),              # 接收 string 类型的 template_id
    name: str = Form(...)                      # 接收 string 类型的 name
):

    """
    根据大纲内容，生成 PPT
    """
    logger.info(f"拿到前端传递的参数")
    # 读取上传的 txt 文件内容
    try:
        text = await file.read()          # 读取文件内容（字节流）
        text = text.decode("utf-8")            # 将字节流解码为字符串
        logger.info(f"传递的txt文本内容为：{text}")
    except Exception:
        Result(400, '文件读取失败', None).to_response()
        raise HTTPException(status_code=400, detail='文件读取失败')

    # 参数校验
    if not all([text, template_id, name]):
        Result(400, '缺少必要参数', None).to_response()
        raise HTTPException(status_code=400, detail='缺少必要参数')

    # 业务逻辑
    out_line = generate_outline(text)
    logger.info(f"生成的outline大纲内容为：{out_line}")
    ppt_data = generate_presentation(out_line, template_id, text, name)

    return Result(200, '生成成功', ppt_data).to_response()



@ppt.get("/")
async def root():
    return {"message": "Hello World"}

